﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using SuvirinimaiApp.Properties;

namespace SuvirinimaiApp
{
    class TvarkytiVadovai : fTvarkytiItems
    {
        public TvarkytiVadovai() :
            base("Darbų vadovai",
            "SELECT Vadovai.id AS ID, Vadovai.vardas AS Vardas, Vadovai.padalinysId, Padaliniai.pavadinimas AS Padalinys, Vadovai.isActive AS Aktyvus " +
                "FROM Vadovai LEFT JOIN Padaliniai ON Vadovai.padalinysId=Padaliniai.id " +
                "ORDER BY Vadovai.isActive, Vadovai.vardas;")
        {}


        protected override void doColumns()
        {
            dgvItems.Columns["Vardas"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dgvItems.Columns["Padalinys"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dgvItems.Columns["padalinysId"].Visible = false;
            dgvItems.Columns["Padalinys"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }

        protected override void buildInsertUpdateCommands()
        {
            dAdapter.UpdateCommand = new OleDbCommand("UPDATE Vadovai SET vardas=@Vardas, padalinysId=@padalinysId, isActive=@Aktyvus WHERE id=@ID;");
            dAdapter.UpdateCommand.Parameters.Add("@Vardas", OleDbType.VarChar, 255, "Vardas");
            dAdapter.UpdateCommand.Parameters.Add("@padalinysId", OleDbType.Integer, 32, "padalinysId");
            dAdapter.UpdateCommand.Parameters.Add("@Aktyvus", OleDbType.Boolean, 32, "Aktyvus");
            dAdapter.UpdateCommand.Parameters.Add("@ID", OleDbType.Integer, 32, "ID");
        }

        protected override void tsbDelete_Click(object sender, EventArgs e)
        {
            string sqlCheckInUse = "SELECT id FROM Aktai WHERE sutvark_vadovasId={0};";
            string sqlDelete = "DELETE FROM Vadovai WHERE id={0};";

            deleteRecord(sqlCheckInUse, sqlDelete);
        }

        protected override void tsbNew_Click(object sender, EventArgs e)
        {
            string[] tekstai = {"Padalinys, rangovas",
                                   "Parinkite tinkamą padalinį arba rangovą:",
                                   "SELECT id AS valueMember, pavadinimas AS displayMember FROM Padaliniai WHERE isActive=True;"};

            if (requestData(RequestScenario.integer_combobox, tekstai) == DialogResult.Cancel) return;
           
            doThings("INSERT INTO Vadovai (padalinysId) VALUES ({0});");
        }
    }
}
